package com.qf.flash.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.event.FuckEvent;
import com.qf.flash.http.response.PlayConditinoResponse;
import com.qf.flash.model.Pay2SponsorBean;
import com.qf.flash.model.ScriptRoomBean;
import com.qf.flash.mvp.contract.ScriptContract;
import com.qf.flash.mvp.presenter.ScriptPrensenter;
import com.qf.flash.ui.home.adapter.HotGridItem;
import com.qf.flash.ui.home.adapter.ScriptRoomItem;
import com.qf.flash.widget.GridSpaceItemDecoration;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.widget.ViewUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2020/3/13 on 22:35
 * 描述:
 * 作者:戴旭东
 */
public class HotActivity extends BaseActivity implements ScriptContract.View {
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.iv_top_right)
    ImageView iv_top_right;
    @BindView(R.id.mRecyclerView)
    RecyclerView rvHot;
    @BindView(R.id.mRefresh)
    SmartRefreshLayout mRefresh;

    private RVAdapter<ScriptRoomBean> mHotAdapter;
    private int spanCount = 3;
    private GridSpaceItemDecoration decoration = new GridSpaceItemDecoration(spanCount, (int) (FlashLib.density * 14), true);
    private ScriptPrensenter mHotPresenter;

    private int page = 1;
    private boolean isRefresh;
    private String templateId;
    private Pay2SponsorBean sponsorBean;

    public static void start(Activity activity, Pay2SponsorBean sponsorBean, String templateId, boolean isRoom) {
        Intent intent = new Intent(activity, HotActivity.class);
        intent.putExtra("isRoom", isRoom);
        intent.putExtra("sponsorBean", sponsorBean);
        intent.putExtra("templateId", templateId);
        activity.startActivity(intent);
    }

    @Override
    protected void initInject() {
        super.initInject();
        mHotPresenter = new ScriptPrensenter();
        initPresenters(mHotPresenter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_hot;
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("热门主题");
        iv_top_right.setColorFilter(Color.BLACK);
        templateId = getIntent().getStringExtra("templateId");
        sponsorBean = getIntent().getParcelableExtra("sponsorBean");
        initRvHot();
        initRefresh();
    }


    private void initRefresh() {
        mRefresh.autoRefresh();
        mRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                requestData(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                requestData(true);
            }
        });
    }


    private void requestData(boolean isRefresh) {
        this.isRefresh = isRefresh;
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("typeId", templateId);
        map.put("isTop", 1);
        map.put("pageNo", page);
        mHotPresenter.getPlayList(map);
    }

    private void initRvHot() {
        GridLayoutManager hotManager = new GridLayoutManager(this, spanCount);
        rvHot.setLayoutManager(hotManager);
        rvHot.addItemDecoration(decoration);
        mHotAdapter = new RVAdapter<>(new HotGridItem(), new ScriptRoomItem(this));
        rvHot.setAdapter(mHotAdapter);
        mHotAdapter.setOnItemClickListener(new RVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (sponsorBean != null) {
                    sponsorBean.setPlayId(mHotAdapter.getData(position).getId());
                    sponsorBean.setPlayName(mHotAdapter.getData(position).getName());
                }

                ScriptDetailActivity.start(HotActivity.this, sponsorBean, position);
            }
        });
    }


    @OnClick({R.id.tv_top_back, R.id.ll_top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.ll_top_right:
                ScriptSearchActivity.start(this, sponsorBean,"请输入剧本名称");
                break;
        }
    }

    @Override
    public void setPlayListData(List<ScriptRoomBean> data, int totalPage) {
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
        if (isRefresh) {
            mHotAdapter.setDatas(data);
        } else {
            mHotAdapter.addData(data);
        }
        mRefresh.setEnableLoadMore(page < totalPage);
    }

    @Override
    public void onError(ApiException e) {
        super.onError(e);
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
    }

    @Override
    public void setConditionData(PlayConditinoResponse response) {

    }

    @Override
    protected boolean useEvent() {
        return true;
    }

    @Subscribe
    public void onFuckEvent(FuckEvent event) {
        if (event.getPosition() != -1) {
            mHotAdapter.setSingleChosenPosition(event.getPosition());
            mHotAdapter.notifyDataSetChanged();
        }
    }
}
